package com.cupcorn.balapkarung.state;

import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ScrollDetector;

import android.view.KeyEvent;

import com.cupcorn.balapkarung.GameDefine;
import com.cupcorn.balapkarung.GameDefineText;
import com.cupcorn.balapkarung.LocalData;
import com.cupcorn.lib.Anchor;
import com.cupcorn.lib.Ratio;
import com.cupcorn.lib.define.iDefines;
import com.cupcorn.lib.display.DisplaySprite;
import com.cupcorn.lib.display.DisplayText;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameState;

public class StateGameOver extends GameState implements GameDefine, GameDefineText
{
	StateGamePlay gamePlay;
	
	DisplaySprite	displaySprite;
	DisplayText		displayText;
	
	public StateGameOver(GameEngine engine)
	{
		super(engine);
	}
	
	public void load()
	{
		displaySprite = new DisplaySprite(engine, GAME_OVER_SPR);
		displayText = new DisplayText(engine, engine.displayFont, TEXT);
	}
	
	public void init()
	{
		for (int i = 0; i < GAME_OVER_SPR.length; i++)
		{
			displaySprite.newSprite(i, engine.hud);
		}
		
		if (LocalData.highscoreOffline < StateGamePlay.score)
		{
			displayText.newText(TEXT_HIGHSCORE, engine.hud);
			LocalData.highscoreOffline = StateGamePlay.score;
		}
		else
		{
			displayText.newText(TEXT_YOUR_SCORE, engine.hud);
		}
		
		displayText.newText(TEXT_SCORE_GAMEOVER, engine.hud);
		displayText.text[TEXT_SCORE_GAMEOVER].setText("" + +StateGamePlay.score);
		displayText.text[TEXT_SCORE_GAMEOVER].setPosition(0, -40, Anchor.CENTER, Ratio.WIDTH);
	}
	
	public void update(float deltaTime)
	{	
		
	}
	
	public void exit()
	{	
		for (int i = 0; i < GAME_OVER_SPR.length; i++)
		{
			displaySprite.getSprite(i).detachSelf();
		}
		
		for (int i = 1; i < TEXT.length; i++)
		{
			if (displayText.text[i] != null) displayText.text[i].detachSelf();
		}
	}
	
	public boolean keyUp(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			engine.playerMusic.stop(MUSIC_INGAME);
			engine.nextState(GameDefine.STATE_MAIN_MENU, iDefines.MESSAGE_STATE_INIT);
			engine.state[STATE_GAME_PLAY].state.exit();
			StateGamePlay.soundIngamePlayed = false;
			
			exit();
		}
		return false;
	}
	
	public void sceneTouch(Scene scene, TouchEvent touchEvent)
	{	
		
	}
	
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		if (pSceneTouchEvent.isActionUp())
		{
			if (pTouchArea.equals(displaySprite.getSprite(GOS_BTN_RESTART)))
			{
				StateGamePlay.isReset = true;
				engine.nextState(STATE_GAME_PLAY, iDefines.MESSAGE_STATE_UPDATE);
				exit();
			}
			// if(pTouchArea.equals(displaySprite.getSprite(GOS_BTN_REPLAY)))
			// {
			// StateGamePlay.isReset = true;
			// StateGamePlay.isReplay = true;
			// backGamePlay();
			// }
			if (pTouchArea.equals(displaySprite.getSprite(GOS_BTN_MENU)))
			{
				engine.playerMusic.stop(MUSIC_INGAME);
				engine.nextState(GameDefine.STATE_MAIN_MENU, iDefines.MESSAGE_STATE_INIT);
				engine.state[STATE_GAME_PLAY].state.exit();
				StateGamePlay.soundIngamePlayed = false;
				
				exit();
			}
		}
		return false;
	}
	
	public void click(ClickDetector clickDetector, int pointerID, float sceneX, float sceneY)
	{	
		
	}
	
	public void scrollStarted(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
	public void Scroll(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
	public void scrollFinished(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
}
